package com.jj.test;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

@Slf4j
public class TypeCastDemo {
    public void hello(){}

    public static <T extends TypeCastDemo & Runnable> void test(T t){
        t.hello();
        t.run();
    }

    public static  <T> T toType(Class<T> type, Object obj){
        T tempObj = null;
        try {
            tempObj = type.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        BeanUtils.copyProperties(obj, tempObj);
        return tempObj;
    }

    public static  <T> T copy(T destObj, Object fromObj){
        BeanUtils.copyProperties(fromObj, destObj);
        return destObj;
    }

    public static void main(String[] args) {
        String msg = "hello";
        Object obj = msg;

        String msg2 = toType(String.class, obj);
        log.debug("msg:{}", msg2);

        Integer num = 1000;
        Short snum = 0;
        obj = copy(snum, num);
        log.debug("num:{}, type:{}", obj, obj.getClass());

//        short snum = (short)1L;
//        obj = snum;
//        log.debug("short num:{}, type:{}", obj, obj.getClass());

    }
}
